import { http } from '@kit.NetworkKit'

let request = http.createHttp()
export function get<T>(url:string,params?:object):Promise<T>{
  return new Promise<T>((resole,reject)=>{
    if (params){
      url += '?' + Object.keys(params).map((k) => `${k} =${params[k]}`).join('&')
    }
    request.request(url, {
      method:http.RequestMethod.GET
    })
      .then((response)=>{
        if (response.responseCode == 200){
          let t:T = JSON.parse(response.result as string)
          resole(t)
        }else {
          reject(new Error(`网络错误${response.responseCode}}`))
        }
      })
      .catch((e:Error) => {
        reject(e)
      })
  })
}